require'copas'
local port=6636
local out=print

local logs={}

local udpinit=function(port)
  local u=socket.udp()
  collectgarbage('collect')
  u:settimeout(0)
  --u:setoption('reuseaddr',true)
  ret,err=u:setsockname("*",port)
  if not ret  then
    out('bind error:'..err)
    concurrent.exit('port:'..port..' bind error.')
  end
  out('udp listening at '..port..'\n')
  out'\n'
  return u;
end

local function udpfini(udpsock)
  udpsock:close()
end

local udpstep=function(udpsock)
  local data,ip,port=udpsock:receivefrom(1024)
  if data then
    out(data)
	logs[#logs+1]=data
  end
end

function logging_event(t)
	table.foreach(t,print)
end

local function dump_logs()

	table.foreach(logs,function(k,l)
		ret,data=pcall(loadstring,l)
		if ret then
			data()
		end

	end

	)

end

local udpsock=udpinit(port)
while true do
	udpstep(udpsock)
	if #logs>1 then dump_logs();break; end
end
